package com.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import com.config.HsProperty;
import com.config.SourceDataProperty;
import com.dto.SourceCarLocationDto;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/***
 * @author: mcq
 * Date : 2024/9/16 
 * 人を信じよ、しかしその百倍も自らを信じよ
 *
 * Desc:  
 */
@Slf4j
@Service
public class HsEncodeUtils {

    @Resource
    private HsProperty hsProperty;

    @Resource
    private SourceDataProperty sourceDataProperty;

    public  ByteBuf packLocationMessage(SourceCarLocationDto sourceCarLocationDto) {
        ByteBuf buffer = Unpooled.buffer();
        try {

            //分割标志
            buffer.writeInt(1213418314);
            //接入号
            buffer.writeInt(hsProperty.getUsername());

            ByteBuf content = Unpooled.buffer();
            //报文类型 1=船舶定位
            content.writeByte(1);
            //信号类型
            content.writeByte(2);

            String terminalId = sourceCarLocationDto.getTid().substring(7);

            content.writeInt(Integer.parseInt(terminalId));

            //经度
            int lng = (int) (sourceCarLocationDto.getLng() * 1000000);
            content.writeInt(lng);
            //纬度
            int lat = (int) (sourceCarLocationDto.getLat() * 1000000);
            ;
            content.writeInt(lat);
            //对地航速 单 位 1/10 节 （ 0-102.2 节 ，1022=102.2 节）
            if (sourceCarLocationDto.getSpeed() != null) {
                double jiespeed = sourceCarLocationDto.getSpeed() / 1.852 * 10;
                content.writeShort((int) jiespeed);
            }else {
                content.writeShort(0);
            }

            //对地航向
            content.writeShort(sourceCarLocationDto.getDirection() * 10);
            //船艏向
            content.writeShort(sourceCarLocationDto.getDirection());
            //英文船名
            byte[] englishNameByteArrays = "".getBytes(CharsetUtil.CHARSET_UTF_8);
            int writeZeroLength = 25 - englishNameByteArrays.length;
            content.writeBytes(englishNameByteArrays);
            if (writeZeroLength > 0) {
                content.writeZero(writeZeroLength);
            }
            String shopChinaNessName = ShopInfo.deviceIds.getOrDefault(sourceCarLocationDto.getTid(), "");

            byte[] chineseNameByteArrays = shopChinaNessName.getBytes(CharsetUtil.CHARSET_GBK);
            writeZeroLength = 25 - chineseNameByteArrays.length;
            content.writeBytes(chineseNameByteArrays);
            if (writeZeroLength > 0) {
                content.writeZero(writeZeroLength);
            }

            //船舶类型 其他
            content.writeByte(5);
            //长度
            content.writeShort(0);
            //宽度
            content.writeByte(0);
            //最大静态吃水 单位 1/10 米（0-255，255=25.5 米或以上）
            content.writeByte(0);

            //单位秒，船舶定位时间
            if (sourceCarLocationDto.getGpstime() != null) {
                Date date = DateUtil.parse(sourceCarLocationDto.getGpstime(),"yyyyMMddHHmmss");
                content.writeInt((int) (date.getTime() / 1000));
            } else {
                int now = (int) (System.currentTimeMillis() / 1000);
                content.writeInt(now);
            }

            //报文长度
            buffer.writeShort(content.readableBytes());
            //内容
            buffer.writeBytes(content);

        } catch (Exception e) {
            log.error("转换为发送的ByteBuf 失败:", e);
            return null;
        }

        return buffer;

    }


    public static void main(String[] args) {
        ByteBuf buffer = Unpooled.buffer();
//        String a = "D5E3B3A4D0CBBBF53135303200000000000000000000000000";
//        String a = "b3a4d0cbbbf520313530320000000000000000000000000000";
//        ByteBuf byteBuf = hexStringToByteBuf(a);
//        CharSequence charSequence = byteBuf.readCharSequence(25, CharsetUtil.CHARSET_GBK);
//        System.err.println(charSequence.toString());
//        Date date = DateUtil.parse(sourceCarLocationDto.getGpstime(),"yyyyMMddHHmmss");
//        System.err.println(date.getTime());
        try {

            //分割标志
            buffer.writeInt(1213418314);
            //接入号
            buffer.writeInt(1520001);

            ByteBuf content = Unpooled.buffer();
            //报文类型 1=船舶定位
            content.writeByte(1);
            //信号类型
            content.writeByte(2);
            //设备 ID
            content.writeInt(12345678);
            //经度
            int lng = 119968525;
            content.writeInt(lng);
            //纬度
            int lat = 30226793;
            content.writeInt(lat);
            //对地航速 单 位 1/10 节 （ 0-102.2 节 ，1022=102.2 节）
            double jiespeed = 102.2 * 10;
            content.writeShort((int) jiespeed);
            //对地航向
            content.writeShort(1250);
            //船艏向
            content.writeShort(120);
            //英文船名
            byte[] englishNameByteArrays = "CHANG XING HUO 1502".getBytes(CharsetUtil.CHARSET_UTF_8);
            int writeZeroLength = 25 - englishNameByteArrays.length;
            content.writeBytes(englishNameByteArrays);
            if (writeZeroLength > 0) {
                content.writeZero(writeZeroLength);
            }

            byte[] chineseNameByteArrays = "浙长兴货1502".getBytes(CharsetUtil.CHARSET_GBK);
            writeZeroLength = 25 - chineseNameByteArrays.length;
            content.writeBytes(chineseNameByteArrays);
            if (writeZeroLength > 0) {
                content.writeZero(writeZeroLength);
            }

            //船舶类型
            content.writeByte(3);
            //长度
            content.writeShort(230);
            //宽度
            content.writeByte(85);
            //最大静态吃水 单位 1/10 米（0-255，255=25.5 米或以上）
            content.writeByte(125);

            //单位秒，船舶定位时间

            content.writeInt(1585108125);
            //报文长度
            buffer.writeShort(content.readableBytes());
            //内容
            buffer.writeBytes(content);

            System.err.println(ByteBufUtil.hexDump(buffer));
        } catch (Exception e) {
            log.error("转换为发送的ByteBuf 失败:", e);
        }

    }


    public static ByteBuf hexStringToByteBuf(String hexString) {
        // 将16进制字符串两个字符一组拆分
        String[] hexBytes = hexString.split("(?<=\\G..)");
        byte[] bytes = new byte[hexBytes.length];
        for (int i = 0; i < hexBytes.length; i++) {
            // 使用Integer.parseInt将16进制字符串转换为整数，再转换为字节
            bytes[i] = (byte) Integer.parseInt(hexBytes[i], 16);
        }
        // 将字节数组包装到ByteBuf中
        return Unpooled.wrappedBuffer(bytes);
    }




}
